package com.notetool.util.tokenparser.syntax;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.notetool.util.tokenparser.ParserSetupException;
import com.notetool.util.tokenparser.WordToken;


public class WordModel extends Model
{
    
    ////////////////////
    // Members
    private Pattern pattern;
    
    ////////////////////
    // Constructors
    public WordModel (String expr)
    {
        
        pattern = compilePattern (expr);
    }
    
    ////////////////////
    // Accessors
    public Pattern getPattern ()
    {
        return pattern;
    }
    
    ////////////////////
    // Model implementation
    
    ////////////////////
    // Private methods
    private Pattern compilePattern (String expr)
    {
        try
        {
            // Compile the pattern
            return Pattern.compile (expr);
        }
        catch (Exception ex)
        {
            throw new ParserSetupException ("Could not build compile WordModel Pattern object.");
        }
    }
    
    ////////////////////
    // Public methods
    public boolean matches (String token)
    {
        return pattern.matcher (token).matches ();
    }
    
    public WordToken instantiate (String content)
    {
        return new WordToken (this, content);
    }
    
    public WordModel addClass (String... clsses)
    {
        for (String curClass : clsses)
            super.addClass (curClass);
        
        return this;
    }

}
